/*
 * Copyright 2010 Assert Developments
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package be.ad.coffeebreak.services;

import be.ad.coffeebreak.dao.UsersDao;
import be.ad.coffeebreak.domain.User;
import be.ad.coffeebreak.exceptions.EmailAlreadyInUseException;
import be.ad.coffeebreak.exceptions.ValidationException;
import be.ad.coffeebreak.validation.ValidationUtils;
import be.ad.coffeebreak.validators.UserValidator;
import org.jboss.annotation.ejb.LocalBinding;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.sql.DataSource;

/**
 * A service for managing security.
 *
 * @author Stefan Bangels
 */
@Stateless
@Local(SecurityService.class)
@LocalBinding(jndiBinding = "/coffeebreak/security/local")
@SuppressWarnings("unused")
public class SecurityServiceBean implements SecurityService {

    @Resource(mappedName = "java:/CoffeeBreakDS")
    protected DataSource dataSource;

    protected UsersDao usersDao;

    /**
     * Initialize the service.
     */
    @PostConstruct
    @SuppressWarnings("unused")
    public void init() {
        usersDao = new UsersDao(dataSource);
    }

    @TransactionAttribute(TransactionAttributeType.SUPPORTS)
    public User authenticateUser(String email, String password) {
        ValidationUtils.assertNotNull(email, "email should not be null");
        ValidationUtils.assertNotNull(password, "password should not be null");
        return usersDao.findUser(email, password);
    }

    public User saveUser(User user) throws EmailAlreadyInUseException, ValidationException {
        ValidationUtils.assertNotNull(user, "user should not be null");
        ValidationUtils.validate(user, new UserValidator(), "invalid user");
        Long id = usersDao.findUserId(user.getEmail());
        if (id != null && !id.equals(user.getId())) {
            throw new EmailAlreadyInUseException("email address already exists!");
        }
        if (user.getId() == null) {
            return usersDao.insertUser(user);
        } else {
            return usersDao.updateUser(user);
        }
    }

    public void changePassword(String email, String newPassword) throws ValidationException {
        ValidationUtils.assertNotNull(email, "email should not be null");
        ValidationUtils.assertNotNull(newPassword, "newPassword should not be null");
        usersDao.changePassword(email, newPassword);
    }

    public void deleteUser(String email) {
        ValidationUtils.assertNotNull(email, "email should not be null");
        usersDao.deleteUser(email);
    }

}
